package com.huanglj.taobaounion.ui.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.huanglj.taobaounion.R;
import com.huanglj.taobaounion.base.BaseFragment;
import com.huanglj.taobaounion.model.domain.Categories;
import com.huanglj.taobaounion.presenter.ISearchPresenter;
import com.huanglj.taobaounion.presenter.implement.HomePresenterImpl;
import com.huanglj.taobaounion.ui.activity.ScannerCodeActivity;
import com.huanglj.taobaounion.ui.adapter.HomeAdapter;
import com.huanglj.taobaounion.utils.LogUtil;
import com.huanglj.taobaounion.utils.PresenterManager;
import com.huanglj.taobaounion.view.IHomeCallBack;
import com.vondear.rxfeature.activity.ActivityScanerCode;

import java.util.List;

import butterknife.BindView;


public class HomeFragment extends BaseFragment implements IHomeCallBack {
    private static final String TAG = "HomeAdapter";

    private HomePresenterImpl mHomePresenter;
    @BindView(R.id.home_search_text)
     EditText mEditText;
    @BindView(R.id.home_tab_layout)
     TabLayout mTabLayout;
    @BindView(R.id.home_view_pager)
     ViewPager mViewPager;
    @BindView(R.id.home_scan)
    ImageView mScanView;
    private HomeAdapter mHomePager;
    private ISearchPresenter mSearchPresenter;

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_home_fragment_layout, container, false);
    }

    @Override
    protected void loadData() {
        mHomePresenter.getCategories();
    }

    @Override
    protected void initView(View view) {
        mTabLayout.setupWithViewPager(mViewPager);
        mHomePager = new HomeAdapter(getChildFragmentManager(),1);
        mViewPager.setAdapter(mHomePager);
        mScanView.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(), ScannerCodeActivity.class);
            startActivityForResult(intent,101);
        });
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (mSearchPresenter != null) {
                        mSearchPresenter.getLoadResult(mEditText.getText().toString().trim());
                    }
                }
                return false;
            }
        });
    }

    @Override
    protected void initPresenter() {
        mHomePresenter = (HomePresenterImpl) PresenterManager.getInstance().getHomePresenter();
        mHomePresenter.registerViewCallBack(this);
        mSearchPresenter = PresenterManager.getInstance().getSearchPresenter();

    }


    @Override
    public int getRes() {
        return R.layout.fragment_home;
    }

    @Override
    public void onCategoriesLoadedSuccess(Categories categories) {

            if (categories != null && categories.getData().size() != 0) {
                List<Categories.DataBean> data = categories.getData();
                LogUtil.d(TAG,data.toString());
                mHomePager.setData(data);
                setUpState(State.SUCCESS);
            }else {
                setUpState(State.EMPTY);
            }
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setUpState(State.EMPTY);
    }

    @Override
    public void onError(String errorMsg) {
        setUpState(State.ERROR);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHomePresenter != null) {
            mHomePresenter.unregisterViewCallBack(this);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case (101):
                Log.d(TAG,"扫码成功"+data.getStringExtra("successCode"));
        }
    }
}
